package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.project.utils.Constants;
import com.project.utils.JExcelUtils;

public class ExportExcel extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public ExportExcel() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    String param =	request.getParameter("param");
	    System.out.println(param);
	    /* 
	    JExcelUtils util = new JExcelUtils();
	    String path =Constants.exportFilePath;
	    String[] titles = {"学号","姓名","语文","数学","英语","总分"};   
	    util.createExcelFile(path, "成绩单", java.util.Arrays.asList(titles));
	    String[] score ={"201000001","潇洒","23.12","78.89","100","590"};
	    List<List<String>> data = new ArrayList<List<String>>();
	    data.add(java.util.Arrays.asList(score));
	    data.add(java.util.Arrays.asList(score));
	    data.add(java.util.Arrays.asList(score));
	    util.insertData(1, data);
	    util.close();
	response.setContentType("application/vnd.ms-excel;charset=gb2312");
	    response.setHeader("Content-Disposition", "attachment; filename=\"" + response.encodeURL(new String("成绩表.xls".getBytes(), "ISO8859_1")) + "\"");
	    File file = new File(path);
	    FileInputStream fis = new FileInputStream(file);
	    OutputStream out = response.getOutputStream();
	    int count;
	    byte[] buffer = new byte[2048];
	    while((count=fis.read(buffer))>=0 )
	    {
	    	out.write(buffer, 0, count);
	    }
	    fis.close();
	    out.flush();
	    out.close();*/
       // Properties prop = new Properties();
		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
	    String[] titles = {"学号","姓名","语文","数学","英语","总分"};  
	    String[] score ={"201000001","潇洒","23.12","78.89","100","590"};
	    List<List<String>> data = new ArrayList<List<String>>();
	    data.add(java.util.Arrays.asList(score));
	    data.add(java.util.Arrays.asList(score));
	    data.add(java.util.Arrays.asList(score));
	    JExcelUtils excelUtil = new JExcelUtils();
	    String contentType = "application/vnd.ms-excel;charset=gb2312";
	    String headerName = "Content-Disposition";
	    String headerValue = "attachment; filename=\"" + response.encodeURL(new String("成绩表.xls".getBytes(), "ISO8859_1")) + "\"";
	    excelUtil.setContentHead(response, contentType, headerName, headerValue);
	    excelUtil.createExcelFile("成绩单",java.util.Arrays.asList(titles));
	    excelUtil.insertData(1, data);
	    excelUtil.write();
	    excelUtil.flushAndClose();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
